// DrawRubberCtrler.cpp: implementation of the CDrawRubberCtrler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../Resource.h"
#include "DrawRubberCtrler.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDrawRubberCtrler::CDrawRubberCtrler( COLORREF color, int width )
: m_x(0), m_y(0)
{
	SetColor (color);		// set the rubber color
	SetPenWidth (width);	// set the rubber width

	m_TaskTyle = ID_RUBBER;	// a rubber
}

CDrawRubberCtrler::~CDrawRubberCtrler()
{
}

void CDrawRubberCtrler::StartPoint(long x, long y)
{
	m_DrawRect.right = m_DrawRect.left = m_x = x;
	m_DrawRect.bottom = m_DrawRect.top = m_y = y;
}

void CDrawRubberCtrler::MoveAt(CDC *pDC, long x, long y)
{
	//
	// Set the CPen //////////////////////////////////////////
	//
	CPen pen(PS_NULL, 1, GetColor());		// NULL pen (draw a rect without border)
	CPen* oldpen = pDC->SelectObject(&pen);

	//
	// Set the CBrush /////////////////////////////////////////
	//
	CBrush brush(GetColor());
	// Set the brush to fill the image.
	CBrush *oldbrush = pDC->SelectObject(&brush);

	// recorde the Draw Rectangle
	m_DrawRect.left = min(m_DrawRect.left, x - GetPenWidth());
	m_DrawRect.right = max(m_DrawRect.right, x + GetPenWidth());
	m_DrawRect.top = min(m_DrawRect.top, y - GetPenWidth());
	m_DrawRect.bottom = max(m_DrawRect.bottom, y + GetPenWidth());

	m_x = x;
	m_y = y;
	pDC->Rectangle(m_x, m_y, m_x + GetPenWidth(), m_y + GetPenWidth());	

	// recover the DC :
	pDC->SelectObject(oldpen);
	pen.DeleteObject();
	pDC->SelectObject(oldbrush);
	brush.DeleteObject();
}